﻿using Huirui.Cavan.Domain.MainBoundedContext.SearchModule;
using Huirui.Cavan.Domain.MainBoundedContext.SystemModule;
using Huirui.Cavan.Domain.Seedwork;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Huirui.Cavan.Core.Extension;

namespace Huirui.Cavan.Application.MainBoundedContext.SystemModule
{
    public class JobAppService : BaseAppService<Job, int, JobSearch, IJobRepository>, IJobAppService
    {
        #region IJobAppService Members

        public override BoolGenericResult<Job> AddOrUpdate(Job entity)
        {
            Init();
            BoolGenericResult<Job> result = new BoolGenericResult<Job>();
            if (entity.IsNull())
            {
                throw new ArgumentNullException("");
            }
            Employee employee = this.EmployeeAppService.GetBySearch(SearchFactory.CreateEmployeeSearch(SecurityAppService.UserAccount.Id)).FirstOrDefault();
            Administrator admin = new Administrator(employee);

            Job oldJob = this.Repository.Get(entity.Id);
            if (oldJob.IsNull())
            {
                result = admin.AddJob(entity.Name, entity.Remark);
                if (result.IsSuccess)
                    return base.Add(result.Object);
            }

            return result;
        }

        #endregion
    }
}
